#include "audioplay.h"


AudioPlay::AudioPlay(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	ui.setupUi(this);

 inputFile.setFileName("ring.pcm");
 inputFile.open(QIODevice::ReadOnly);

 QAudioFormat format;
 // Set up the format, eg.
 format.setFrequency(8000);
 format.setChannels(1);
 format.setSampleSize(16);
 format.setCodec("audio/pcm");
 format.setByteOrder(QAudioFormat::LittleEndian);
 format.setSampleType(QAudioFormat::UnSignedInt);

 QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
 if (!info.isFormatSupported(format)) {
     //qWarning()<<"raw audio format not supported by backend, cannot play audio.";
     return;
 }

 audio = new QAudioOutput(format, this);
 connect(audio,SIGNAL(stateChanged(QAudio::State)),SLOT(finishedPlaying(QAudio::State)));
 audio->start(&inputFile);
}

 void AudioPlay::finishedPlaying(QAudio::State state)
 {
   if(state == QAudio::IdleState) {
     audio->stop();
     inputFile.close();
     delete audio;
   }
 }

AudioPlay::~AudioPlay()
{

}
